// 服务员查询
var SEARCH = {},
    CONFIG;
var cityList, multiSelector, $confirmBtn;
// 渲染查询界面
var renderSearch = function() {
    for (var key in SEARCH) {
        var $subTitle = $api.dom('#' + key + ' .subtitle');
        $api.text($subTitle, SEARCH[key].join(','));
    }
};
// 打开查询界面
var openSearchInfo = function(type) {
    var content = [];
    if (type == 'trades') {
		CONFIG[type].some(function(item, i) {
			content.push(item.name);
		});
	} else {
        content = CONFIG[type];
    }
	var selectedItems = [];
	var searchValues = SEARCH[type];
	if (searchValues && searchValues.length) {
		content.some(function(config, i) {
			searchValues.some(function(selected, j) {
				if (config == selected) {
					selectedItems.push(i);
				}
			});
		});
	}

	multiSelector.open({
        content: content,
		selectedItems: selectedItems
    }, function(ret, err) {
		SEARCH[type] = ret.selectAry;
		renderSearch();
    });
};
// 打开城市列表
var openCityList = function() {
    cityList.open({
        x: 0,
        y: 0,
        width: api.frameWidth,
        height: api.frameHeight,
        currentCity: SEARCH.hometown || '',
        resource: 'widget://res/citylist.json'
    }, function(ret, err) {
        if (ret) {
            SEARCH.hometown = [ret.cityInfo.city];
            renderSearch();
            cityList.close();
        } else {
            api.alert({
                msg: '服务端错误。' + err.msg
            });
        }
    });
};
// 获取配置
var getConfigs = function(callback) {
    api.ajax({
        url: SERVER_URL + '/entity/config',
        method: 'get',
        timeout: 30,
        dataType: 'json',
        cache: false
    }, callback);
};
// 页面加载完成
var apiready = function() {
    multiSelector = api.require('multiSelector');
    cityList = api.require('cityList');
	$confirmBtn = $api.dom('button#confirm');

    /* 一个获取导航栏高度的方法. */
    window.getNavHeight = function() {
        var h = 44;

        if ("ios" == api.systemType && parseInt(api.systemVersion) >= 7) {
            h = 64;
        }

        return h;
    }

    /* 一个关闭窗口,返回上一级页面的优雅实现. */
    window.closeWin = function(winName) {
        api.closeWin();
    }

    // -----------------------------------------
    /* 导航栏自适应iOS 7. */
    var header = $api.byId('header');
    $api.fixIos7Bar(header);

    /* Android,点击返回键,可以返回上一级. */
    window.addEventListener("keyback", function() {
        window.addEventListener("keyback", function() {
            if (!api["pageParam"]["isFrame"]) { // 仅在是主窗口时,才捕捉 keyback事件.
                window.closeWin();
            }
        });
    });

	SEARCH = api.pageParam || {};

    // 如果有默认工种，则不能修改
    if (SEARCH.trades) {
        $api.remove($api.dom('#trades .arrow'));
        $api.dom('#trades').onclick = function() {};
    }

    getConfigs(function(config, err) {
        if (config) {
            CONFIG = config;
			$api.addEvt($confirmBtn, 'click', function() {
				api.execScript({
					name : 'waiters',
					script : 'search(' + JSON.stringify(SEARCH) + ');'
				});
				window.closeWin();
			});
            renderSearch();
        } else {
            api.alert({
                msg: err.msg
            });
        }
    });

};
